import java.util.HashMap;
import java.util.Map;

//连续数组
public class demo21 {
    class Solution {
        public int findMaxLength(int[] nums) {
            int maxlen = 0;
            int counter = 0;    //计数
            Map<Integer,Integer> map = new HashMap<Integer,Integer>();
            map.put(counter,-1);
            int n = nums.length;
            for(int i = 0; i < n; i++)
            {
                if(nums[i] == 1)
                {
                    counter++;
                }
                else{
                    counter--;
                }
                if(map.containsKey(counter))
                {
                    int num = map.get(counter);
                    maxlen = Math.max(i-num,maxlen);
                }
                else{
                    map.put(counter,i);
                }
            }
            return maxlen;
        }
    }
}
